package com.biostime.qdingding.modules.qd.web;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.service.BaseService;
import com.biostime.qdingding.common.utils.DateUtils;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Coursesection;
import com.biostime.qdingding.modules.qd.service.CoursesectionService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 上课时间阶段Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/coursesection")
public class CoursesectionController extends BaseController {

	@Autowired
	private CoursesectionService coursesectionService;
	
	@ModelAttribute
	public Coursesection get(@RequestParam(required=false) String id) {
		Coursesection entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = coursesectionService.get(id);
		}
		if (entity == null){
			entity = new Coursesection();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:coursesection:view")
	@RequestMapping(value = {"list", ""})
	public String list(Coursesection coursesection, HttpServletRequest request, HttpServletResponse response, Model model) {
		String str = request.getParameter("starton");
		if(str != null){
			try {
				coursesection.setStarton(DateUtils.parseDate(str,new String[]{"HH:mm"}));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		BaseService.dataScopeFilter(coursesection, "dsf", "id=a.center_id", "");
		Page<Coursesection> page = coursesectionService.findPage(new Page<Coursesection>(request, response), coursesection); 
		model.addAttribute("page", page);
		return "modules/qd/coursesectionList";
	}

	@RequiresPermissions("qd:coursesection:view")
	@RequestMapping(value = "form")
	public String form(Coursesection coursesection, Model model) {
		model.addAttribute("coursesection", coursesection);
		return "modules/qd/coursesectionForm";
	}

	@RequiresPermissions(value={"qd:coursesection:edit","qd:coursesection:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,Coursesection coursesection, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, coursesection)){
			return form(coursesection, model);
		}
		try {
			coursesection.setStarton(DateUtils.parseDate(request.getParameter("starton"),new String[]{"HH:mm"}));
			coursesection.setEndon(DateUtils.parseDate(request.getParameter("endon"),new String[]{"HH:mm"}));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		User curUser = UserUtils.getUser();
		coursesection.setCenter(curUser.getCenter());
		
		coursesectionService.save(coursesection);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursesection/?repage";
	}
	
	@RequiresPermissions("qd:coursesection:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Coursesection coursesection, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				coursesectionService.delete(new Coursesection(id));
			}
		}else{
			coursesectionService.delete(coursesection);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursesection/?repage";
	}

}